#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

float cal_test()
{
    float sum = 0.0;
    float val;
    for (int i = 0; i < 100000000; i++)
    {
        val = i * 1.0;
        sum += val * val;
    }

    return sum;
}

int main(int argc, char *argv[])
{
    struct timeval start_tv, end_tv, cpu_tv;
    gettimeofday(&start_tv, NULL);

    // cpu计算
    float result = cal_test();

    gettimeofday(&end_tv, NULL);
    timersub(&end_tv, &start_tv, &cpu_tv);
    double cpu_time = cpu_tv.tv_sec + cpu_tv.tv_usec / 1000000.0;
    printf("result=%f, cpu time: %lf\n", result, cpu_time);

    return 0;
}
